# -*- coding: utf-8 -*-
"""
Created on Sat Dec 21 14:30:33 2019

@author: JimmyMo
"""

import pandas as pd
import numpy as np
import time

filepath = "D:/projects/amazon_s3/category=10079992011_raw.csv"
filepath = "D:/projects/amazon_analysis/emr/steps/v1_data/data/Analytics/optimalPrice/BestSellers/batch20190930_testing/category=1063292/part-00000-91c81187-d32d-4752-8d9a-8e90ff74acf0.c000.csv"
data = pd.read_csv(filepath)
df = data[['rank', 'price']]

asin_col = data['asin']
print(df)

print(df.head(4))
print(df.describe())
#df.boxplot()

from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler().fit(df.values)

scaled_data = scaler.transform(df.values)
#unscaled_data1 = scaler.inverse_transform(scaled_data1)

#scaled_data = preprocessing.scale(df.values)
#print("StandardScaler...")
#print(scaled_data)

fit_data = scaled_data
#fit_data = df.values

from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

startTm = time.time()
model = KMeans(n_clusters=3, init='random', random_state=9).fit(fit_data)
centers = np.array(model.cluster_centers_)
y_pred = model.predict(fit_data)
endTm = time.time()

print("cost %s ms" % (endTm - startTm))

plt.xlabel("ranking")
plt.ylabel("price")


unscaled_fit_data = scaler.inverse_transform(fit_data)
unscaled_centers = scaler.inverse_transform(centers)
plt.scatter(unscaled_fit_data[:, 0], unscaled_fit_data[:, 1], c=y_pred)
plt.scatter(unscaled_centers[:,0], unscaled_centers[:,1], marker="x", color='r')
#plt.scatter(fit_data[:, 0], fit_data[:, 1], c=y_pred)
plt.show()

for cluster in range(0, model.n_clusters):
    print("cluster %s, centroids price=%s" % (cluster, unscaled_centers[cluster][1]))
    print("cluster %s, centroids ranking=%s" % (cluster, unscaled_centers[cluster][0]))

print(y_pred)

df['p'] = pd.Series(y_pred, index=df.index)
df['asin'] = asin_col
df.to_csv('D:/projects/amazon_s3/optimalPrice1_pred.csv')